﻿<UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" 
    xmlns:Majesty_of_Omega_GUI_Controller="clr-namespace:Majesty_of_Omega.GUI.Controller" 
    xmlns:Parts="clr-namespace:Majesty_of_Omega.GUI.Pages.Game.Research.Parts" 
    xmlns:Majesty_of_Omega_ValueConverters="clr-namespace:Majesty_of_Omega.GUI.ValueConverters"
    xmlns:Majesty_of_Omega_GUI="clr-namespace:Majesty_of_Omega.GUI" 
    x:Class="Majesty_of_Omega.GUI.Pages.Game.Research.Parts.TechnologyMatrixPart"
    d:DesignWidth="970.5" d:DesignHeight="631.5"
    Background="Black"
    >

    <UserControl.Resources>

        <Majesty_of_Omega_ValueConverters:TechLevelConverter x:Key="TechLevelConverter"   />
        <Majesty_of_Omega_ValueConverters:TechIconToImageConverter x:Key="TechIconToImageConverter"/>

        <Style x:Key="TechSchoolButtonStyle" TargetType="{x:Type RadioButton}">
            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
            <Setter Property="Background" Value="#F4F4F4"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type RadioButton}" >
                        <ControlTemplate.Resources>
                            <Storyboard x:Key="EnterTechSchoolStoryboard">
                                <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="border" Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[1].(GradientStop.Color)">
                                    <SplineColorKeyFrame KeyTime="00:00:00" Value="#FF232D43"/>
                                    <SplineColorKeyFrame KeyTime="00:00:00.2000000" Value="#FF5B72A5"/>
                                </ColorAnimationUsingKeyFrames>
                                <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="border" Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[0].(GradientStop.Offset)">
                                    <SplineDoubleKeyFrame KeyTime="00:00:00" Value="0.005"/>
                                    <SplineDoubleKeyFrame KeyTime="00:00:00.2000000" Value="0.004"/>
                                </DoubleAnimationUsingKeyFrames>
                                <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="border" Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[0].(GradientStop.Color)">
                                    <SplineColorKeyFrame KeyTime="00:00:00" Value="#FF8995B2"/>
                                    <SplineColorKeyFrame KeyTime="00:00:00.2000000" Value="#FFCFDDFF"/>
                                </ColorAnimationUsingKeyFrames>
                            </Storyboard>
                            <Storyboard x:Key="LeaveTechSchoolStoryboard">
                                <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="border" Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[1].(GradientStop.Color)">
                                    <SplineColorKeyFrame KeyTime="00:00:00" Value="#FF5B72A5"/>
                                    <SplineColorKeyFrame KeyTime="00:00:00.2000000" Value="#FF232D43"/>
                                </ColorAnimationUsingKeyFrames>
                                <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="border" Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[0].(GradientStop.Offset)">
                                    <SplineDoubleKeyFrame KeyTime="00:00:00" Value="0.004"/>
                                    <SplineDoubleKeyFrame KeyTime="00:00:00.2000000" Value="0.005"/>
                                </DoubleAnimationUsingKeyFrames>
                                <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="border" Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[0].(GradientStop.Color)">
                                    <SplineColorKeyFrame KeyTime="00:00:00" Value="#FFCFDDFF"/>
                                    <SplineColorKeyFrame KeyTime="00:00:00.2000000" Value="#FF8995B2"/>
                                </ColorAnimationUsingKeyFrames>
                            </Storyboard>
                        </ControlTemplate.Resources>
                        <Border Width="Auto" Height="Auto" Margin="8,4,8,4" BorderBrush="#FFFFFFFF" BorderThickness="2,2,2,2" x:Name="border">
                            <Border.Background>
                                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                    <GradientStop Color="#FF8995B2" Offset="0.005"/>
                                    <GradientStop Color="#FF232D43" Offset="0.994"/>
                                </LinearGradientBrush>
                            </Border.Background>
                            <Grid Background="{x:Null}" Width="Auto" Height="Auto"  >
                                <ContentPresenter x:Name="presenter"  HorizontalAlignment="Stretch" Margin="{TemplateBinding Padding}" 
                                                  VerticalAlignment="Stretch" RecognizesAccessKey="True" 
                                                  TextElement.FontWeight="Bold"
                                                  />
                            </Grid>
                        </Border>
                        <ControlTemplate.Triggers>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="IsMouseOver" Value="True" />
                                    <Condition Property="IsChecked" Value="False" />
                                </MultiTrigger.Conditions>
                                <MultiTrigger.ExitActions>
                                    <BeginStoryboard Storyboard="{StaticResource LeaveTechSchoolStoryboard}" />
                                </MultiTrigger.ExitActions>
                                <MultiTrigger.EnterActions>
                                    <RemoveStoryboard BeginStoryboardName="IsCheckedExitStory"/>
                                    <BeginStoryboard x:Name="MouseOverEnterStoryBoard" Storyboard="{StaticResource EnterTechSchoolStoryboard}"/>
                                </MultiTrigger.EnterActions>
                            </MultiTrigger>
                            <Trigger Property="IsChecked" Value="True">
                                <Trigger.ExitActions>
                                    <BeginStoryboard  x:Name="IsCheckedExitStory" Storyboard="{StaticResource LeaveTechSchoolStoryboard}" />
                                </Trigger.ExitActions>
                                <Trigger.EnterActions>
                                    <BeginStoryboard Storyboard="{StaticResource EnterTechSchoolStoryboard}"/>
                                </Trigger.EnterActions>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <DataTemplate x:Key="TechSchoolDataTemplate">
            <RadioButton x:Name="radioButton" Style="{DynamicResource TechSchoolButtonStyle}" 
						 GroupName="TechSchoolButtons"
                         VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch" 
                         Width="Auto" Height="Auto" IsChecked="{Binding Path=IsSelected, Mode=Default}">
                <TextBlock x:Name="textBlock" Text="{Binding Path=TechSchoolResearch.TechSchool.Name, Mode=Default}" 
						   TextWrapping="Wrap" VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="#FFFFFFFF"/>
            </RadioButton>
        </DataTemplate>
        <ItemsPanelTemplate x:Key="TechSchoolItemsPanelTemplate">
            <UniformGrid Columns="{Binding Path=TechSchoolResearchList.Count, Mode=Default}" Rows="1"/>
        </ItemsPanelTemplate>

        <!-- 
            DataTemplate to draw the whole research technology application for all visible tech levels for a technology field
         -->
        <DataTemplate x:Key="TechFieldResearchDataTemplate">
            <Grid Height="Auto" Background="{x:Null}">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="180"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <Grid HorizontalAlignment="Stretch" x:Name="TechFieldGrid" Height="Auto" VerticalAlignment="Top">
                	<Grid.ColumnDefinitions>
                		<ColumnDefinition Width="0.675*"/>
                		<ColumnDefinition Width="0.325*"/>
                	</Grid.ColumnDefinitions>
            		<Grid.RowDefinitions>
            			<RowDefinition Height="Auto" />
            			<RowDefinition Height="Auto" MinHeight="80"/>
            			<RowDefinition Height="Auto" />
            			<RowDefinition Height="Auto" />
            			<RowDefinition Height="Auto" />
            		</Grid.RowDefinitions>
            		<TextBlock x:Name="textBlock" HorizontalAlignment="Left" RenderTransformOrigin="0.5,0.5" Text="{Binding Path=TechField.Name, Mode=Default}" TextWrapping="Wrap" Grid.RowSpan="1" Background="{x:Null}" Foreground="#FFFFFFFF" Grid.Row="0" VerticalAlignment="Top" Height="Auto" Margin="8,4,0,0" FontWeight="Bold" Grid.ColumnSpan="2"/>
            		<Image x:Name="image" Source="{Binding Path=Icon, Converter={StaticResource TechIconToImageConverter}, Mode=Default}" Grid.RowSpan="1" Grid.Row="1"  Width="132" Height="82" Grid.ColumnSpan="2" Margin="8,0,8,0"/>

                    <!-- labels -->
            		<TextBlock x:Name="textBlock1" Grid.Row="2" Text="Technology Level" TextWrapping="Wrap" Margin="8,2,0,2" VerticalAlignment="Top" Height="Auto" Foreground="#FFFFFFFF" FontSize="10" HorizontalAlignment="Left" Grid.ColumnSpan="1" />
            		<TextBlock x:Name="textBlock2" Grid.Row="3" Text="Research Spendage" TextWrapping="Wrap" Margin="8,2,0,2" Height="Auto" VerticalAlignment="Top" Foreground="#FFFFFFFF" HorizontalAlignment="Left" FontSize="10" />
            		<TextBlock x:Name="textBlock3" Grid.Row="4" Text="Research progress til next level" TextWrapping="Wrap" Margin="8,2,0,2" Foreground="#FFFFFFFF" Grid.RowSpan="2" VerticalAlignment="Top" HorizontalAlignment="Left" FontSize="10" Grid.ColumnSpan="1" d:LayoutOverrides="VerticalAlignment, GridBox" />

					<!-- valuses -->
            		<TextBlock x:Name="textBlock4" Grid.Row="2" Grid.Column="1" Text="{Binding Path=TechLevel, StringFormat=Level \{0\}}" TextWrapping="Wrap" Margin="8,0,0,0" VerticalAlignment="Bottom" Height="Auto" Grid.ColumnSpan="1" Foreground="#FFFFFFFF" FontSize="10" />
                    <TextBlock x:Name="textBlock5" Grid.Row="3" Grid.Column="1" Text="{Binding Path=CurrentResearchFormatted}"  TextWrapping="Wrap" Margin="8,4,0,0" Height="15.96" Grid.ColumnSpan="1" VerticalAlignment="Top" Foreground="#FFFFFFFF" FontSize="10" />
                    <DockPanel Grid.Row="4" Grid.Column="1" Margin="8,0,0,4"  Grid.ColumnSpan="1" >
            			<TextBlock x:Name="textBlock6" Text="{Binding Path=ResearchDoneFormatted}" TextWrapping="Wrap" Width="Auto" Height="Auto" VerticalAlignment="Center" Foreground="#FFFFFFFF" FontSize="10" />
            			<ProgressBar Value="{Binding Path=ProgressInCurrentLevel, Mode=OneWay}"  Width="Auto" Height="20" DockPanel.Dock="Right" HorizontalAlignment="Stretch" Margin="16,0,8,0"/>
            		</DockPanel>
            	</Grid>
            	<ItemsControl Background="{x:Null}" x:Name="TechLevelAppList" 
            		ItemsPanel="{DynamicResource ResearchLevelItemsPanelTemplate}" 
            		ItemTemplate="{DynamicResource ResearchLevelCellTemplate}" 
                    Grid.Column="1" 
					AlternationCount="2"> 

            		<!-- The Techlevel Converter needs two parameters: the tech field and the first techlevel -->
            		<ItemsControl.ItemsSource>
            			<MultiBinding Converter="{StaticResource TechLevelConverter}">
            				<Binding  />
            				<Binding ElementName="TechLevelScrollbar" Path="Value" />
            			</MultiBinding>
            		</ItemsControl.ItemsSource>
            	</ItemsControl>
            	<Path Stretch="Fill" HorizontalAlignment="Stretch" Width="Auto" Grid.Column="1" Data="M0,0 L100,0" VerticalAlignment="Bottom" Margin="-7.5,1,0,0" StrokeThickness="4" Grid.ColumnSpan="3" Grid.Row="4">
            		<Path.Stroke>
            			<LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
            				<GradientStop Color="#FF000000" Offset="0"/>
            				<GradientStop Color="#FF000000" Offset="1"/>
            				<GradientStop Color="#FF7F7F7F" Offset="0.5"/>
            			</LinearGradientBrush>
            		</Path.Stroke>
            	</Path>
            </Grid>
        </DataTemplate>
        
        <ItemsPanelTemplate x:Key="ResearchLevelItemsPanelTemplate">
            <StackPanel Orientation="Horizontal"/>
        </ItemsPanelTemplate>
        <LinearGradientBrush x:Key="AlternateLevelBrush" EndPoint="1,0.5" StartPoint="0,0.5">
        	<GradientStop Color="#FF2D2D2D" Offset="0"/>
        	<GradientStop Color="#FF373737" Offset="0.985"/>
        	<GradientStop Color="#FF2B2A2A" Offset="0.525"/>
        </LinearGradientBrush>

        <!--
            DataTemplate for with a List of all Technologies for a Technology Level in a tech Field.
            There could be many Technology Applications for some Tech Levels
        -->
        <DataTemplate x:Key="ResearchLevelCellTemplate" >
            <Grid x:Name="ResearchLevelCell" Width="180" Height="Auto" MinWidth="0" MinHeight="0" Opacity="1" >
                <Parts:ResearchedTechProgressBarControl Grid.Column="1" Percentage="{Binding ProgressInPercent}" />
                <ItemsControl HorizontalAlignment="Stretch" Width="Auto" x:Name="TechApplicationsControl"  
        			ItemsSource="{Binding Path=TechApplicationStateInfos, Mode=Default}">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <Parts:TechMatrixApplicationPart />
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
                <Rectangle x:Name="ResearchCellBackground" Stroke="#FF000000" Fill="{x:Null}" StrokeThickness="0"/>
            </Grid>
            <DataTemplate.Triggers>
                <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                	<Setter Property="Fill" TargetName="ResearchCellBackground" Value="#FF4B4B4B"/>
                	<Setter Property="Opacity" TargetName="ResearchCellBackground" Value="0.25"/>
                </Trigger>
            </DataTemplate.Triggers>
        </DataTemplate>

        <Style TargetType="{x:Type HeaderedItemsControl}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type HeaderedItemsControl}">
                        <Grid >
                            <Grid.RowDefinitions>
                                <RowDefinition Height="32"/>
                                <RowDefinition Height="*"/>
                            </Grid.RowDefinitions>
							<ContentPresenter ContentSource="Header"/>
							<ScrollViewer Grid.Row="1">
                                	<ItemsPresenter/>
							</ScrollViewer>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <DataTemplate x:Key="TechListLevelHeader">
        	<Border Width="Auto" Height="Auto" BorderThickness="0,0,0,2" BorderBrush="{DynamicResource DefaultedBorderBrush}" Background="{DynamicResource DefaultedBorderBrush}">
        		<Grid Width="Auto" Height="Auto">
        			<Grid.ColumnDefinitions>
        				<ColumnDefinition Width="180"/>
        				<ColumnDefinition Width="*"/>
        			</Grid.ColumnDefinitions>
        			<ItemsControl Grid.Column="2" 
        				ItemsPanel="{DynamicResource ResearchLevelItemsPanelTemplate}" 
        				ItemsSource="{Binding}" 
        				ItemTemplate="{DynamicResource LevelNumberDataTemplate}"  />
        			<!-- <TextBlock Margin="8,8,27,8" TextWrapping="Wrap" Text="Technology Level" Foreground="#FFF2F2F2" /> -->
        		</Grid>
        	</Border>
        </DataTemplate>
        <DataTemplate x:Key="LevelNumberDataTemplate" >
        	<Border Width="180" Height="Auto" BorderBrush="#FF605F5F" BorderThickness="1,1,1,1">
        		<Grid x:Name="LevelNumberCell" Width="Auto" Height="Auto">
        			<TextBlock x:Name="textBlock" HorizontalAlignment="Center" Width="Auto" Text="{Binding}" TextWrapping="Wrap" Foreground="#FFFFFFFF" VerticalAlignment="Center" FontWeight="Bold" FontStretch="ExtraExpanded" RenderTransformOrigin="0.5,0.5" FontSize="16">
        				<TextBlock.RenderTransform>
        					<TransformGroup>
        						<ScaleTransform ScaleX="1.95" ScaleY="1"/>
        						<SkewTransform AngleX="0" AngleY="0"/>
        						<RotateTransform Angle="0"/>
        						<TranslateTransform X="0" Y="0"/>
        					</TransformGroup>
        				</TextBlock.RenderTransform>
        			</TextBlock>
        		</Grid>
        	</Border>
            <DataTemplate.Triggers>
                <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                	<Setter Property="Background" TargetName="LevelNumberCell" Value="{DynamicResource AlternateLevelBrush}"/>
                </Trigger>
            </DataTemplate.Triggers>
        </DataTemplate>
        <SolidColorBrush x:Key="ResearchMatrixBackgroundBrush" Color="#FF3A3838"/>


    </UserControl.Resources>

    <!---
        The TechMatrixController spends all Data for this page. 
      -->
    <UserControl.DataContext>
        <Majesty_of_Omega_GUI_Controller:TechMatrixController/>
    </UserControl.DataContext>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="45"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="39"/>
        </Grid.RowDefinitions>
        <ItemsControl x:Name="TechSchoolButtons" ItemsSource="{Binding Path=TechSchoolResearchList, Mode=Default}" ItemTemplate="{DynamicResource TechSchoolDataTemplate}" ItemsPanel="{DynamicResource TechSchoolItemsPanelTemplate}" />
        <HeaderedItemsControl x:Name="TechListForSchool" 
        	Background="{DynamicResource ResearchMatrixBackgroundBrush}" 
        	HeaderTemplate="{DynamicResource TechListLevelHeader}"
            Header="{Binding Path=LevelNumbers}"
			ItemsSource="{Binding Path=ResearchedTechFields, Mode=Default}" 
        	ItemTemplate="{DynamicResource TechFieldResearchDataTemplate}" 
        	Width="Auto" Height="Auto" Grid.Row="1" Grid.RowSpan="1" 
			/>
        <ScrollBar x:Name="TechLevelScrollbar" HorizontalAlignment="Stretch" 
                   Value="{Binding Path=FirstVisibleLevel}"
                   Margin="61,8,0,8" Width="Auto" Grid.Row="2" Orientation="Horizontal" 
                   Maximum="99" SmallChange="1" />
    </Grid>
</UserControl>
